#include <Zigbee.h>

void UART8_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));//????8????????

u8 Uart8_Rx_State = 0;//????8????????????????zigbee???
u8 Uart8_Rx_Buf[10]={0};//????8???????????????
u8 Uart_Rx_State;


void Uart8_Init()   //Zigbee???
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef  NVIC_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8 , ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;//4TX 5RX
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOC, &GPIO_InitStructure);

    USART_InitStructure.USART_BaudRate = 115200;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;

    USART_Init(UART8, &USART_InitStructure);

    USART_ITConfig(UART8, USART_IT_RXNE,ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    USART_Cmd(UART8, ENABLE);
}

void Zigbee_Send(u8 addr,u8* string)
{
    u8 addr1=0x00,addr2=0x00;
    switch(addr)
    {
        case 0:
            addr1=0x00;
            addr2=0x00;
        break;
        case 1://10 E8
              addr1=0x10;
              addr2=0xE8;
              //????1?????
        break;
        case 2://33 0C
              addr1=0x33;
              addr2=0x0C;
              //????2??????
        break;
    }


    USART_SendData(UART8, 0x55);//???
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);

    USART_SendData(UART8, 0x00);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, 0x44);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);//????

    USART_SendData(UART8, 0x00);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, 0x10);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);//????


    u8 temp=0x00;
    temp=0x00^0x44^0x00^0x10^0x02^addr1^addr2^0x01^0x01^0x0A^string[0]^string[1]^string[2]^string[3]^string[4]^string[5]^string[6]^string[7]^string[8]^string[9];
    USART_SendData(UART8, temp);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);//????


    USART_SendData(UART8, 0x02);//??????????
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);

    USART_SendData(UART8, addr1);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, addr2);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);//????


    USART_SendData(UART8, 0x01);//????
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, 0x01);//?????
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);

    USART_SendData(UART8, 0x0A);//??????????
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);


    USART_SendData(UART8, string[0]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[1]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[2]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[3]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[4]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[5]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[6]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[7]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[8]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);
    USART_SendData(UART8, string[9]);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);//????

    USART_SendData(UART8, 0xAA);
    while(USART_GetFlagStatus(UART8,USART_FLAG_TC)==0);//????

}

void Zigbee_Send_Data(u8 heartRate)
{
    // ???:
    // Zigbee_String[0] = 1  ????????????????????
    // Zigbee_String[1] = heartRate  ????????????
    Zigbee_String[0] = 1;
    Zigbee_String[1] = 2;
    Zigbee_String[2] = DropRate;

    // ?????????????????????????????????? 6
    Zigbee_Send(0, Zigbee_String);
}

/*zigbee?????????*/
void UART8_IRQHandler(void)
{
     u8 temp;
    if(USART_GetITStatus(UART8, USART_IT_RXNE) != RESET)
       {
            temp=USART_ReceiveData(UART8);

            switch(Uart8_Rx_State)
            {
                case 0:
                      if(temp==0x55)Uart8_Rx_State++;
                      else Uart8_Rx_State=0;
                break;
                case 1:
                      if(temp==0xFF)Uart8_Rx_State++;
                      else Uart8_Rx_State=0;
                break;
                case 2:
                      if(temp==0x01)Uart8_Rx_State++;
                      else Uart8_Rx_State=0;
                break;
                case 3:
                    Uart8_Rx_State++;
                break;
                case 4:
                    Uart8_Rx_State++;
                break;
                case 5:
                    Uart8_Rx_State++;
                break;
                case 6:
                     Uart8_Rx_Buf[0]=temp;
                     Uart8_Rx_State++;
                break;
                case 7:
                     Uart8_Rx_Buf[1]=temp;
                     Uart8_Rx_State++;
                break;
                case 8:
                    if(Uart8_Rx_Buf[0]==1)
                    {   if(Uart8_Rx_Buf[1]==1)//?????????????????????
                          {
                              n_heart_rate = temp;
//                            temperature_o=temp*100;
                          }
                    }
                     Uart8_Rx_State++;
                break;
                case 9:
                     if(Uart8_Rx_Buf[0]==1)
                     {
                       if(Uart8_Rx_Buf[1]==1)
                      {
                            n_spo2 = temp;
                      }
                     }

                     Uart8_Rx_State++;
                break;
                case 10:
                    Uart8_Rx_State++;
                    Uart8_Rx_State=0;
                break;
            }
       }
    USART_ClearITPendingBit(UART8, USART_IT_RXNE);
}

